# -*- coding: utf-8 -*-
# @Time    : 2020/6/22 8:40
# @Author  : ZHALONG
# @FileName: 猫眼电影Top100.py
# @Software: PyCharm

import requests
import time
from parsel import Selector
import csv

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36"
}
def get_rseponse(url):
    """

    :param url: 抓取的链接
    :return: 链接内容
    """
    try:
        response = requests.get(url, headers=headers)
        response.encoding = "utf-8"
        response.raise_for_status()
        time.sleep(1)
        return response
    except Exception as e:
        return e.args

def parse(response):
    """

    :param response:
    :param writer:
    """
    selector = Selector(response.text)
    dd_list = selector.xpath("//dl[@class='board-wrapper']/dd")
    for dd in dd_list:
        title = dd.xpath("./a/@title").get()
        star_ = dd.xpath(".//p[@class='star']/text()").get()
        star = star_.split("：")[1].strip()
        time_ = dd.xpath(".//p[@class='releasetime']/text()").get()
        time = time_.split("上映时间：")[1]
        score_ = dd.xpath(".//p[@class='score']//i/text()").getall()
        score = "".join(score_)
        print(title, star, time, score)
        writer.writerow([title, star, time, score])

if __name__ == '__main__':
    file = open("猫眼榜单Top100.csv", "a", encoding="utf-8", newline="")
    writer = csv.writer(file)
    writer.writerow(["标题","主演","上映时间","评分"])

    for page in range(0, 100, 10):
        url = f"https://maoyan.com/board/4?offset={page}"
        response = get_rseponse(url)
        parse(response)







